import 'dart:math';

import 'package:flutter/material.dart';
import 'package:zhou_po/assets/color.dart';
import 'package:zhou_po/component/home_btn.dart';
import 'package:zhou_po/component/home_info_card.dart';
import 'package:zhou_po/component/login.dart';
import 'package:zhou_po/forum.dart';

void main() {
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: '多彩周坡',
      debugShowCheckedModeBanner: false,
      theme: ThemeData(
        colorScheme: ColorScheme.fromSeed(seedColor: Colors.lightBlue),
        useMaterial3: true,
        appBarTheme: AppBarTheme(
          surfaceTintColor: Colors.transparent,
          scrolledUnderElevation: 0,
        ),
      ),
      home: const MyHomePage(title: '多彩周坡'),
    );
  }
}

class MyHomePage extends StatefulWidget {
  const MyHomePage({super.key, required this.title});

  final String title;

  @override
  State<MyHomePage> createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  int tabIndex = 0;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: tabIndex == 1
          ? Color(0xffAF0505)
          : (tabIndex == 2 ? Color(0xffFFE7DC) : os_back),
      appBar: AppBar(
        toolbarHeight: tabIndex == 0 ? null : 0,
        backgroundColor: tabIndex == 1
            ? Color(0xffAF0505)
            : (tabIndex == 2 ? Color(0xffFFE7DC) : os_back),
        title: Row(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            Text(
              tabIndex == 0 ? "多彩周坡 · 数字化平台" : "",
              style: TextStyle(
                color: Colors.black,
                fontSize: 14,
                fontWeight: FontWeight.normal,
              ),
            ),
          ],
        ),
      ),
      body: IndexedStack(
        index: tabIndex,
        children: [
          ListView(
            children: [
              HomeBtn(),
              InfoTag(),
              ...forums.map(
                (e) => HomeInfoCard(
                  title: e["title"],
                  date: e["date"],
                  imgUrl: e["imgUrl"],
                  linkUrl: e["linkUrl"],
                ),
              ),
              Container(
                margin: EdgeInsets.only(top: 25, bottom: 15),
                child: Text(
                  "更多推送消息请关注「多彩周坡」微信公众号",
                  textAlign: TextAlign.center,
                  style: TextStyle(
                    color: Color(0xff666666),
                  ),
                ),
              ),
              // Technically supported by USTC
              Container(
                margin: EdgeInsets.only(top: 0, bottom: 25),
                child: Text(
                  "Technically supported by USTC",
                  textAlign: TextAlign.center,
                  style: TextStyle(
                    color: Color(0xff666666),
                  ),
                ),
              ),
            ],
          ),
          ListView(
            children: [
              Row(
                children: [
                  Expanded(
                    child: Image.asset(
                      "lib/img/sell.png",
                    ),
                  ),
                ],
              ),
            ],
          ),
          ListView(
            children: [
              Row(
                children: [
                  Expanded(
                    child: Image.asset(
                      "lib/img/camera.png",
                    ),
                  ),
                ],
              ),
            ],
          ),
          ListView(
            children: [
              LoginPage(),
              Container(
                margin: EdgeInsets.only(top: 25, bottom: 25),
                child: Text(
                  "Technically supported by USTC",
                  textAlign: TextAlign.center,
                  style: TextStyle(
                    color: Color(0xff666666),
                  ),
                ),
              ),
            ],
          ),
        ],
      ),
      bottomNavigationBar: BottomNavigationBar(
        currentIndex: tabIndex,
        unselectedFontSize: 13,
        selectedFontSize: 13,
        selectedItemColor: os_color,
        unselectedItemColor: Colors.grey[600],
        type: BottomNavigationBarType.fixed,
        onTap: (idx) {
          setState(() {
            tabIndex = idx;
          });
        },
        items: [
          BottomNavigationBarItem(
            icon: Icon(Icons.home),
            label: "主页",
          ),
          BottomNavigationBarItem(
            icon: Icon(Icons.sell),
            label: "农副销售",
          ),
          BottomNavigationBarItem(
            icon: Icon(Icons.camera_outdoor_rounded),
            label: "智慧天眼",
          ),
          BottomNavigationBarItem(
            icon: Icon(Icons.person),
            label: "我",
          ),
        ],
      ),
    );
  }
}

class InfoTag extends StatelessWidget {
  const InfoTag({
    super.key,
  });

  @override
  Widget build(BuildContext context) {
    return Container(
      margin: EdgeInsets.only(top: 5, bottom: 5),
      child: Text(
        "- 最新推送 -",
        textAlign: TextAlign.center,
        style: TextStyle(
          color: Colors.grey[600],
        ),
      ),
    );
  }
}
